﻿using System.Linq;
using Tools.Domain.Common.Repositories.Contracts;
using Tools.Domain.Specifications;
using Webshop20.Domain.Customers.Core;
using Webshop20.Model.Customers.Specifications;

namespace Webshop20.Domain.Customers.Specifications
{
    public class EMailAdressUniqueSpecification<T> : Specification<T> 
    {
        private IRepository<Customer> customerRepository;

        public EMailAdressUniqueSpecification(IRepository<Customer> customerRepository)
            : base(
                customerToVerify => customerRepository.Find(
                                        customerInRepo => customerInRepo.EMail == ((IEMailAdressUnderSpecificationValidation)customerToVerify).EMail && customerInRepo.Id != ((IEMailAdressUnderSpecificationValidation)customerToVerify).Id
                                        )
                                        .Count() == 0,
                "EMAIL_NOT_UNIQUE")
        {
            this.customerRepository = customerRepository;
        }
    }
}